using System;
using System.Collections.Generic;
using System.Text;
using DAL;
using DTO;
using System.Data;
using System.Data.OleDb;

namespace BLL
{
    public class MonhocDAL
    {
        public List<MONHOC> GetAll()
        {
            MONHOC monhoc;
            List<MONHOC> list = new List<MONHOC>();
            DataProvider dataprovider = new DataProvider();
            DataTable datatable = new DataTable();
            datatable = dataprovider.ExecuteSelectQuery("Select * from MONHOC", null);
            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                monhoc = new MONHOC();
                monhoc.Mamon = Convert.ToByte(datatable.Rows[i]["Mamon"]);
                monhoc.Tenmon = datatable.Rows[i]["Tenmon"].ToString();
                list.Add(monhoc);
            }
            return list;
        }

        public MONHOC GetMONHOC(byte mamon)
        {
            DataProvider dataprovider = new DataProvider();
            OleDbParameter[] parameters = new OleDbParameter[1];
            parameters[0] = new OleDbParameter("@mamon", OleDbType.TinyInt);
            parameters[0].Value = mamon;
            DataTable datatable = new DataTable();
            datatable = dataprovider.ExecuteSelectQuery("Select * from MONHOC where MAMON = @mamon", parameters);
            MONHOC monhoc = new MONHOC();
            if (datatable.Rows.Count != 0)
            {
                monhoc.Mamon = Convert.ToByte(datatable.Rows[0]["Mamon"]);
                monhoc.Tenmon = datatable.Rows[0]["Tenmon"].ToString();
                return monhoc;
            }
            else
                return null;
        }

        public bool UpdateMONHOC(byte mamon, string tenmon, byte mamoncu)
        {
            DataProvider dataprovider = new DataProvider();
            OleDbParameter[] parameters = new OleDbParameter[3];
            parameters[0] = new OleDbParameter("@mamon", OleDbType.TinyInt);
            parameters[0].Value = mamon;
            parameters[1] = new OleDbParameter("@tenmon", OleDbType.Char);
            parameters[1].Value = tenmon;
            parameters[2] = new OleDbParameter("@mamoncu", OleDbType.TinyInt);
            parameters[2].Value = mamoncu;
            return dataprovider.ExecuteNonSelectQuery("update MONHOC set Mamon=@mamon, Tenmon=@tenmon where Mamon = @mamoncu", parameters);
        }

        public bool AddMONHOC(int mamon, string ten)
        {
            /*INSERT INTO table_name
            VALUES (value1, value2, value3,...)*/

            DataProvider dataprovider = new DataProvider();
            OleDbParameter[] parameters = new OleDbParameter[2];
            parameters[0] = new OleDbParameter("@mamon", OleDbType.SmallInt);
            parameters[0].Value = mamon;
            parameters[1] = new OleDbParameter("@tenmon", OleDbType.Char);
            parameters[1].Value = ten;
            return dataprovider.ExecuteNonSelectQuery("INSERT INTO MONHOC (MAMON,TENMON) VALUES (@mamon,@ten) ", parameters);
        }
        public bool DELETEMONHOC(int mamon)
        {
            /*INSERT INTO table_name
            VALUES (value1, value2, value3,...)*/

            DataProvider dataprovider = new DataProvider();
            OleDbParameter[] parameters = new OleDbParameter[1];
            parameters[0] = new OleDbParameter("@mamon", OleDbType.SmallInt);
            parameters[0].Value = mamon;
            
            return dataprovider.ExecuteNonSelectQuery("DELETE FROM MONHOC WHERE MAMON=@mamon ", parameters);
        }
    }
}